package com.it.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        log.info("----------开始创建 Redis 模版对象----------");
        /*
        * TODO 设置key的序列化器
        *      默认的Key序列化器为：JdkSerializationRedisSerializer
        *      StringRedisSerializer支持字符串类型的转化，而且默认使用UTF-8编码
        *      下面代码的意思是使用 StringRedisSerializer 序列化器替换默认的Key序列化器JdkSerializationRedisSerializer
        * */
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setKeySerializer(new StringRedisSerializer());

//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
}
